package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.bean.UserMixtureBean;
import com.koron.css2.serviceManage.mapper.UserMixtureMapper;
import com.koron.css2.serviceManage.queryBean.UserMixtureQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.UserMixtureVO;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class UserMinxtureList implements ServerInterface{
	private Logger log = LogManager.getLogger(UserMinxtureList.class);
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<UserMixtureVO> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", UserMixtureVO.class);
		try {
			UserMixtureQueryBean queryBean = JsonUtils.objectToPojo(req.getData(),UserMixtureQueryBean.class);
			UserMixtureMapper mapper = factory.getMapper(UserMixtureMapper.class);
			List<UserMixtureBean> list = new ArrayList();
			if("2".equals(queryBean.getType()))
				list = mapper.listTemp(queryBean);
			else
				list = mapper.list(queryBean);
			if(list !=null && list.size()>0) {
				for(UserMixtureBean bean : list) {
					bean.setWaterRatio(subZeroAndDot(bean.getWaterRatio()));
				}
			}

			UserMixtureVO vo = new UserMixtureVO();
			vo.setList(list);
			info.setData(vo);
		}catch(Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			log.error(e.getMessage(), e);
		}
		return info;
	}
	
	/**
	 * 使用java正则表达式去掉多余的.与0
	 * 
	 * @param s
	 * @return
	 */
	public static String subZeroAndDot(String s) {
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

}
